< previous page page_368 next page >

Page 368
As you can see, adding an event is really easy. To access your events, the calling code must declare objects using the WithEvents keyword and have an event procedure written that the object can call. For example, placing the following code in the General Declarations section of a form or module would trap the DataError event:
Dim WithEvents objBoss As clsEmployee
Private Sub objBoss_DataError(ByVal sMessageText As String)
    MsgBox sMessageText, vbCritical, "Error!!!"
End Sub
The WithEvents keyword causes the name of the object to be added to the object drop-down box so that Visual Basic can create the skeleton event procedure for you.
To test this with the sample, create the DataError event in the class module and add code to trap the event as described above. You should then be able to cause the object to fire the event by calling the CalcRaise method when the Salary property is equal to zero.
Creating an ActiveX DLL
Although including classes in Standard EXE projects is useful, compiling them into a separate ActiveX DLL or ActiveX EXE component is even more useful. These ActiveX components can be installed and registered on another machine so that applications on that computer can access your classes. For example, you could create a database access DLL and install it on the Web server for use with Active Server Pages. The following sections describe some of the steps involved in creating an ActiveX project.
Creating an ActiveX Project
To create an ActiveX DLL, you need to add your class to an ActiveX DLL project. The easiest way to create a project of this type is to choose ActiveX DLL from the New Project dialog box. As you may recall, when you select a new Standard EXE project, you are given a project with a form named Form1. Similarly, creating a new ActiveX DLL or EXE project causes VB to create a project with a class module called Class1. If you are starting a new class, just rename Class1 to a better name and begin entering code. If you want to add class modules to the project that you have already written, right-click in the Project Explorer window and choose Add File.
note.gif ActiveX DLL and EXE projects can contain multiple class modules, code modules, and forms. Only the objects designated as public are visible to other applications. square.gif

Working with Multiple Projects
In the first part of this chapter, you added a class module to a Standard EXE project. Although the class module was fairly self-contained, it was still a part of the same project as the form. However, when working with an ActiveX DLL project, you will more than likely need an additional Standard EXE project to test your classes because an ActiveX DLL is intended for use from an outside application. In other words, the application's user interface might be one

 
< previous page page_368 next page >